package com.qbm.association.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qbm.association.bean.ResultBean;
import com.qbm.association.bean.UserTypeEnum;
import com.qbm.association.config.Audience;
import com.qbm.association.entity.Administrator;
import com.qbm.association.entity.Student;
import com.qbm.association.service.AdministratorService;
import com.qbm.association.service.PasswordService;
import com.qbm.association.service.StudentService;
import com.qbm.association.util.JWTTokenUtil;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @author: Aron
 * @date: 2021.12.10
 */
@Api(tags = "登录控制层")
@RestController
@CrossOrigin
@RequestMapping("/association/login/")
public class LoginController {

    @Resource
    private PasswordService passwordService;

    @Resource
    private StudentService studentService;

    @Resource
    private AdministratorService administratorService;

    @Autowired
    private Audience audience;

    @GetMapping("hello")
    public String hello(){
        return "hello";
    }

    @GetMapping("studentLogin")
    public ResultBean studentLogin(String username, String password){
        Student student = studentService.getByUsername(username);
        System.out.println(username);
        Assert.notNull(student,"用户名或密码错误");
        boolean isTrue = passwordService.matchPassword(student.getId(), UserTypeEnum.STUDENT,password);
        Assert.isTrue(isTrue,"用户名或密码错误");
        String token = JWTTokenUtil.createJWT(username,UserTypeEnum.STUDENT.getCode(), audience);
        Map<String,Object> map = new HashMap<>();
        map.put("token",token);
        map.put("id",student.getId());
        return ResultBean.success(map,"登录成功");
    }

    @GetMapping("adminLogin")
    public ResultBean adminLogin(String username, String password){
        Administrator administrator = administratorService.getByUsername(username);
        Assert.notNull(administrator,"用户名或密码错误");
        boolean isTrue = passwordService.matchPassword(administrator.getId(), UserTypeEnum.ADMIN,password);
        Assert.isTrue(isTrue,"用户名或密码错误");
        String token = JWTTokenUtil.createJWT(username,UserTypeEnum.ADMIN.getCode(), audience);
        return ResultBean.success(token,"登录成功");
    }

    @PostMapping("studentRegister")
    public ResultBean studentRegister(Student student,String password) {
        Student temp = studentService.getByUsername(student.getUserName());
        Assert.isNull(temp,"该用户名已存在，请重新输入");
        studentService.register(student,password);
        return ResultBean.success("注册成功");
    }

    @PostMapping("adminRegister")
    public ResultBean studentRegister(Administrator administrator,String password) {
        Administrator temp = administratorService.getByUsername(administrator.getUserName());
        Assert.isNull(temp,"该用户名已存在，请重新输入");
        administratorService.register(administrator,password);
        return ResultBean.success("注册成功");
    }

}
